package controller;

import java.io.IOException;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AlunoDAO;
import dao.MateriaDAO;
import dao.ProfessorDAO;
import dao.TurmaDAO;
import entity.Aluno;
import entity.Materia;
import entity.Professor;
import entity.Turma;
import controller.UsuarioControler;

@WebServlet(name = "/TurmaController", urlPatterns = { "/turmaList",
		"/turmaFrmInsert", "/turmaInsert", "/turmaUpdate", "/turmaDelete" })
public class TurmaController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		List<Professor> professores = null;
		List<Aluno> alunos = null;
		List<Materia> materias = null;

		switch (action) {

		case "confirmDelete":
			String par = request.getParameter("nomeTurma");
			request.setAttribute("nomeTurma", par);
			request.getRequestDispatcher(
					"/view/private/turma/confirmDelete.jsp").forward(request,
					response);
			break;

		case "frmInsert":
			materias = MateriaDAO.getAll();
			request.setAttribute("listaMaterias", materias);

			professores = ProfessorDAO.getAll();
			request.setAttribute("listaProfessores", professores);

			alunos = AlunoDAO.getAll();
			request.setAttribute("listaAlunos", alunos);

			request.getRequestDispatcher("/view/private/turma/cadastro.jsp")
					.forward(request, response);

			break;

		case "frmUpdate":

			String nomeTurma = request.getParameter("nomeTurma");
			request.setAttribute("nomeTurma", nomeTurma);

			materias = MateriaDAO.getAll();
			request.setAttribute("listaMaterias", materias);

			professores = ProfessorDAO.getAll();
			request.setAttribute("listaProfessores", professores);

			alunos = AlunoDAO.getAll();
			request.setAttribute("listaAlunos", alunos);

			request.getRequestDispatcher("/view/private/turma/altera.jsp")
					.forward(request, response);

			break;

		case "list":
			Integer totalLogados = (Integer) getServletContext().getAttribute(
					"qde_Users");
			request.setAttribute("totalLogados", totalLogados);
			
			List<Turma> turmas = TurmaDAO.getAll();

			request.setAttribute("lista", turmas);
			request.getRequestDispatcher("/view/private/turma/lista.jsp")
					.forward(request, response);
			break;

		case "altera":

			turmas = TurmaDAO.findById(new Integer(request.getParameter("id")));

			request.setAttribute("lista", turmas);
			request.getRequestDispatcher("/view/private/turma/altera.jsp")
					.forward(request, response);

			break;

		case "delete":

			if (TurmaDAO.delete(request.getParameter("nomeTurma"))) {
				response.sendRedirect("" + request.getContextPath()
						+ "/turmaList?action=list");
			} else {
				response.sendRedirect("" + request.getContextPath()
						+ "/turmaList?action=list");
			}
			break;
		}

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");
		Turma turma = new Turma();

		switch (action) {

		case "insert":
			String[] ids = request.getParameterValues("id");

			Professor professor = ProfessorDAO.findByIdRetObj(new Integer(
					request.getParameter("idProfessor")));
			Materia materia = MateriaDAO.findByIdRetObj(new Integer(request
					.getParameter("idMateria")));

			Aluno aluno = new Aluno();

			turma.setNome(request.getParameter("nomeTurma"));

			for (int i = 0; i < ids.length; i++) {
				aluno.setMatricula(new Integer(ids[i]));
				if (!TurmaDAO.insert(turma, professor, aluno, materia)) {
					response.sendRedirect("" + request.getContextPath()
							+ "/turmaList?action=list");
					break;
				}
			}
			
			response.sendRedirect("" + request.getContextPath()
					+ "/turmaList?action=list");

			break;

		case "update":

			break;
		}
	}
}
